package com.tjp.eight.ui.fragment.setpassword;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tjp.eight.LoginEvent;
import com.tjp.eight.MainActivity;
import com.tjp.eight.R;
import com.tjp.eight.RxBus;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.TimeDown;

/**
 * Created by Administrator on 2017/3/3.
 */
public class FragmentSetPass extends BaseFragment<SetPassPresenter> implements SetPassView {
    private String phone = "";
    private EditText et_reg_set_pass;
    private EditText et_set_p_code;
    private TextView tv_reg_next;
    private TextView tv_set_p;
    private TextView tv_tips;
    private TimeDown timeDown;

    @Override
    protected SetPassPresenter createPresenter() {
        return new SetPassPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_set_pass, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timeDown.cancel();
    }

    @Override
    public void setPass(SetPassModel setPassModel) {
        if ("success".equals(setPassModel.result)) {
            SharePreferencesUtils.setLoginKey(setPassModel.loginkey);
            SharePreferencesUtils.setUid(setPassModel.uid);
            // getActivity().setResult(0x01);
            AppManager.getAppManager().notFinishActivity(MainActivity.class);
            LoginEvent loginEvent = LoginEvent.getLoginEvent();
            loginEvent.login = true;
            RxBus.getInstance().post(loginEvent);
        } else {
            Toast.makeText(getContext(), "" + setPassModel.msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(getContext(), "" + msg, Toast.LENGTH_SHORT).show();
    }

    void initViews() {
        phone = getActivity().getIntent().getStringExtra("phone");
        tv_tips = (TextView) contentView.findViewById(R.id.tv_tips);
        tv_tips.setText("验证码短信已经发至+86 " + phone);
        et_reg_set_pass = (EditText) contentView.findViewById(R.id.et_reg_set_pass);
        et_set_p_code = (EditText) contentView.findViewById(R.id.et_set_p_code);
        et_set_p_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changNextState();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_reg_set_pass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(" ")) {
                    String[] str = s.toString().split(" ");
                    String str1 = "";
                    for (int i = 0; i < str.length; i++) {
                        str1 += str[i];
                    }
                    et_reg_set_pass.setText(str1);

                    et_reg_set_pass.setSelection(start);

                }
                changNextState();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        tv_reg_next = (TextView) contentView.findViewById(R.id.tv_reg_next);
        tv_set_p = (TextView) contentView.findViewById(R.id.tv_set_p);

        timeDown();
        tv_set_p.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeDown();
            }
        });

        tv_reg_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetPModel setPModel = new SetPModel();
                setPModel.mobile = phone;
                setPModel.mobilecode = et_set_p_code.getText().toString();
                setPModel.pwd = et_reg_set_pass.getText().toString();
                Gson gson = new Gson();
                String json = gson.toJson(setPModel);
                presenter.setPass(json);
            }
        });
    }

    void changNextState() {
        if (et_reg_set_pass.getText().toString().length() >= 6 && et_set_p_code.getText().toString().length() == 6) {
            tv_reg_next.setEnabled(true);
        } else {
            tv_reg_next.setEnabled(false);
        }
    }

    void timeDown() {
        tv_set_p.setEnabled(false);
        if (timeDown == null)
            timeDown = new TimeDown(60, new TimeDown.TimeDownListener() {
                @Override
                public void onPrepareCountDown() {

                }

                @Override
                public void onRefreshTime(int second) {
                    tv_set_p.setText(second + "秒");
                }

                @Override
                public void onFinishCountDown() {
                    tv_set_p.setText("重新获取");
                    tv_set_p.setEnabled(true);
                    timeDown = null;
                }
            });
        timeDown.start();
    }
}
